<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no"/>
    <title>用户注册</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css"/>
    <style>
        body {
            background-color: #F3F3F3;
        }

        .aui-content {
            margin-top: 3px;
        }

        .aui-btn-block {
            margin-top: 5px;
            padding: 8px 0;
            font-size: 18px;
        }

        .aui-input-row {
            /*margin: 0 18px;*/
        }

        .aui-input-row {
            border-top: 0px solid #c8c7cc;
        }

        .aui-input-row:first-child:after {
            border-top: 0px solid #c8c7cc;
        }

        .aui-btn-row:after {
            border-bottom: 0px solid #c8c7cc;
        }

        .border_r {
            height: 100%;
            border-right: 1px solid #EBEBEB;
            padding:0 15px !important;
        }

        .br {
            padding-top: 10px;
        }
    </style>
</head>
<body>
<div class="br"></div>
<div class="aui-content">
    <div class="aui-form">
        <div class="aui-input-row">
            <span class="aui-input-addon border_r">手机号</span>
            <input type="tel" id="mobile" class="aui-input" placeholder="请输入手机号"/>
        </div>
        <div class="aui-input-row">
            <span class="aui-input-addon border_r">验证码</span>
            <input type="number" id="verify" class="aui-input" placeholder="请输入验证码"/>
					<span class="aui-input-addon">
						<div class="aui-btn" id="sendVerify" onclick="getVerify()">
                            获取验证码
                        </div> </span>
        </div>
        <div class="aui-input-row">
            <span class="aui-input-addon border_r">密　码</span>
            <input type="password" id="password" class="aui-input" placeholder="请输入密码"/>
        </div>

    </div>
    <div class="aui-btn-row">
        <div class="aui-btn aui-btn-info aui-btn-block" tapmode onclick="writeData();">
            提交
        </div>
    </div>
</div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.11.3.js"></script>
<script type="text/javascript">
    var isinerval, times;
    apiready = function () {
        api.parseTapmode();
    }
    function closeWin() {
        api.closeWin();
    }
    function writeData() {
        $('.aui-form input').blur();
        
        var mobile = $api.val($api.byId('mobile'));
        var vrify = $api.val($api.byId('verify'));
        var password = $api.val($api.byId('password'));
        if (mobile.length > 0 && vrify.length > 0 && password.length < 6) {
            api.toast({
                msg: '密码长度不能少于6位',
                duration: 2000,
                location: 'bottom'
            });
        } else if (mobile.length > 0 && vrify.length > 0 && password.length > 0) {
            //接收验证码后重新验证手机号
            var partten = /^1[3-9]\d{9}$/;
            if (!partten.test(mobile)) {
                api.toast({
                    msg: '请输入正确的手机号',
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            };
            $('input').each(function(){
                $(this).blur();
            })
            api.showProgress({
                style: 'default',
                animationType: 'fade',
                title: '正在注册...',
                text: '',
                modal: true
            });
            var url = 'register/verifyMobile/mobile/' + mobile;
            ajaxRequest(url, 'get', '', function (ret, err) {
                if (ret.status == '0') {
                    api.hideProgress();
                    api.toast({
                        msg: '该手机号已被注册',
                        duration: 2000,
                        location: 'bottom'
                    });
                } else {
                    var data = {};
                    if (mobile.length > 0 && vrify.length > 0 && password.length > 0) {
                        data['mobile'] = mobile;
                        data['password'] = password;
                        data['verify'] = vrify;
                        data['code'] = '';
                        var url = 'register/memberadd';
                        ajaxRequest(url, 'post', data, function (ret, err) {
                            if (ret.status == 1) {
                                // api.toast({
                                //     msg: '注册成功',
                                //     duration: 2000,
                                //     location: 'bottom'
                                // });
                                // $api.clearStorage();
                                // $api.setStorage('user_id', '' + ret.msg.id + '');
                                // $api.setStorage('group_id', '' + ret.msg.group_id + '');
                                // $api.setStorage('user_name', '' + ret.msg.user_name + '');
                                // $api.setStorage('nick_name', '' + ret.msg.nick_name + '');
                                // $api.setStorage('birthday', '' + ret.msg.birthday + '');
                                // $api.setStorage('sex', '' + ret.msg.sex + '');
                                // $api.setStorage('avatar', '' + ret.msg.avatar + '');

                                $api.clearStorage();
                                $api.setStorage('user_id', '' + ret.msg.userid + '');
                                $api.setStorage('group_id', '' + ret.msg.groupid + '');
                                $api.setStorage('user_name', '' + ret.msg.username + '');
                                $api.setStorage('nickname', '' + ret.msg.username + '');
                                $api.setStorage('company', '' + ret.msg.company + '');
                                $api.setStorage('money', '' + ret.msg.money + '');
                                $api.setStorage('online', '' + ret.msg.online + '');
                                $api.setStorage('message', '' + ret.msg.message + '');
                                $api.setStorage('head_pic', '' + ret.msg.head_portrait + '');
                                $api.setStorage('chat', '' + ret.msg.chat + '');
                                $api.setStorage('credit', '' + ret.msg.credit + '');
                                $api.setStorage('collect', '' + ret.msg.collect + '');
                                $api.setStorage('rongcloud_token', '' + ret.msg.rongcloud_token + '');
                                // api.toast({
                                //     msg: '注册成功',
                                //     duration: 2000,
                                //     location: 'bottom'
                                // });
                                
                                api.sendEvent({
                                    name: 'reGetMyInfo',
                                    extra: {
                                        key: 'true'
                                    }
                                })
                                setTimeout(function () {
                                    api.hideProgress();
                                    api.closeToWin({
                                        name: 'root',
                                        animation: {
                                            type: 'flip',
                                            subType: 'from_bottom',
                                            duration: 500
                                        }
                                    });
                                }, 1000)
                            } else if (ret.status == 2) {
                                api.hideProgress();
                                api.alert({
                                    msg: ret.msg,
                                    buttons: ['OK']
                                }, function (ret, err) {
                                    setTimeout(function () {
                                        api.closeToWin({
                                            name: 'login_win',
                                            animation: {
                                                type: 'flip',
                                                subType: 'from_bottom',
                                                duration: 500
                                            }
                                        });
                                    }, 1000)
                                });
                            } else {
                                api.hideProgress();
                                api.toast({
                                    msg: ret.msg,
                                    duration: 2000,
                                    location: 'bottom'
                                })
                            }
                        })
                    } else {
                        api.toast({
                            msg: '请输入完整信息',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            });
        } else {
            api.toast({
                msg: '请输入完整信息',
                duration: 2000,
                location: 'bottom'
            });
        }
    }

    //获取验证码
    function getVerify() {
        var mobile = $api.val($api.byId('mobile'));
        var partten = /^1[3-9]\d{9}$/;
        if (!partten.test(mobile)) {
            api.toast({
                msg: '请输入正确的手机号',
                duration: 2000,
                location: 'bottom'
            });
            return false;
        }
        //验证手机是否注册过
        var url = 'register/verifyMobile/mobile/' + mobile;
        ajaxRequest(url, 'get', '', function (ret, err) {
            if (ret.status == '0') {
                api.toast({
                    msg: '该手机号已被注册',
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            } else {
                $('#verify').get(0).focus();
                //发送验证码
                var url = 'register/verifyMobiled/mobile/' + mobile;
                ajaxRequest(url, 'get', '', function (ret, err) {
                    if (ret.status == 1) {
                        var sendVerify = $api.byId('sendVerify');
                        $api.removeAttr(sendVerify, 'onclick');
                        //提交成功前不可更改，防止用发送后用其他号码注册 或者 在服务端验证
                        var readonly_mobile = $api.byId('mobile');
                        $api.attr(readonly_mobile, 'readOnly', 'true');
                        $api.html(sendVerify, '已发送(<span id="GetVerify">60</span>)');
                        times = 60;
                        isinerval = setInterval("CountDown()", 1000);
                    } else {
                        api.toast({
                            msg: '短信发送失败',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
            }
        });
    }

    function CountDown() {
        if (times < 1) {
            //提交成功前不可更改，防止用户发送后用其他号码注册 或者 在服务端验证
            var readonly_mobile = $api.byId('mobile');
            $api.removeAttr(readonly_mobile, 'readOnly');
            var sendVerify = $api.byId('sendVerify');
            $api.attr(sendVerify, 'onclick', 'getVerify()');
            $api.html(sendVerify, '重新获取');
            clearInterval(isinerval);
            return;
        }
        var getVerify = $api.byId('GetVerify');
        $api.html(getVerify, '' + times + '');
        times--;
    }
</script>
</html>